<?php
include 'header.php';
@$type = $_REQUEST['type'];
?>
		<div class='container'>
   			<div class='row'>
   				<div class='col-xs-2'>
   					<a href='index.php' class='btn btn-default btn-block'>返回首页</a>
   					<?php if($type == "worker"){?>
   						<a href='workerList.php?type=role' class='btn btn-default btn-block'>职位列表</a>
   					<?php }else if($type == "role"){?>
   						<a href='workerList.php?type=worker' class='btn btn-default btn-block'>职员列表</a>
   					<?php }?>
   					<a href='addGoodsType.php?addType=worker' class='btn btn-default btn-block'>添加职员</a>
   					<a href='addGoodsType.php?addType=role' class='btn btn-default btn-block'>添加职位</a>
   				</div>
   				<?php if($type == "worker"){?>
   					<div class='col-xs-10'>
   				<?php }else if($type == "role"){?>
   					<div class='col-xs-6'>
   				<?php }?>
   				
   					<div class='panel panel-primary'>
   						<div class='panel-heading'>
   							<?php if($type == "worker"){?>
   								<div class='panel-title'>所有职员列表</div>
   							<?php }else if($type == "role"){?>
   								<div class='panel-title'>所有职位列表</div>
   							<?php }?>
   						</div>
   						<table class='workerTable table table-bordered'>
   							<?php if($type == "worker"){?>
	   							<tr>
	   								<th>姓名</th>
	   								<th>头像</th>
	   								<th>年龄</th>
	   								<th>性别</th>
	   								<th>生份证</th>
	   								<th>联系电话</th>
	   								<th>家庭住址</th>
	   								<th>职位</th>
	   								<th>编辑</th>
	   							</tr>
   							<?php }else if($type == "role"){?>
   								<tr>
   									<th>名字</th>
   									<th>编辑</th>
   								</tr>
   							<?php }?>
   						</table>
   					</div>
   				</div>
   			</div>
   		</div>
   		<script>
   			var type = "<?php echo $type;?>";
	   		var workerTable = $('.workerTable').get(0);
	  		var workerTableHeader = workerTable.innerHTML;
	  		listType();
	  		function listType(){
	  			workerTable.innerHTML = workerTableHeader;
	  			$.post('workerData.php',{'type':type},function(o){
	  				if(!o.status){
	  					$.message('消息',o.info);
	  				}else{
	  					var workers = o.workers;
	  					var roles = o.roles;
	  					if(type == "worker"){
		  					for(var i=0; i<workers.length; i++){
		  						var worker = workers[i];
		  						var row = workerTable.insertRow(-1);
		  						eachWorkers(worker,row);
		  					}
	  					}else if(type == "role"){
	  						for(var i=0; i<roles.length; i++){
		  						var role = roles[i];
		  						var row = workerTable.insertRow(-1);
		  						eachRoles(role,row);
		  					}
		  				}
	  				}
	  			});
	  		}
	  		function eachRoles(role,row){							//职位列表
				var nameSpan = $('<span>',{html:role.name});
				nameSpan.editable({
					placement:'bottom',
					type:'text',
					url:'upWorkerImg.php',
					name:'role',
					pk:role.id
				});
				nameSpan.appendTo(row.insertCell(-1));
				
				var deleteSpan = $('<span>',{html:'删除'});			//删除
      			deleteSpan.addClass('delWorker');
      			deleteSpan.click(function(){
      				$.confirm('删除确认',"你确定要删除["+role.name+"]?");
      				$('#confirmBtn').click(function(){
          				var index = row.rowIndex;
      					$.post('del.php',{'id':role.id,'name':'role'},function(o){
      						if(!o.status){
      							$.message('消息',o.info);
      						}else{
      							workerTable.deleteRow(index);
      							dialog.modal('hide');
      						}
      					});
      				});
      			});
      			deleteSpan.addClass('deleteSpan').appendTo(row.insertCell(-1));
		  	}
	  		function eachWorkers(worker,row){
	  			var imgSpan = $('<span>');						//图片
      			imgSpan.appendTo(row.insertCell(-1));
      			var img = $("<img src='img/worker/"+worker.imgUrl+"'>");
      			img.addClass('workerImg');
      			img.addClass('icon'+worker.wId);
      			img.appendTo(imgSpan);
      			$('.icon'+worker.wId).click(function(){
      				$.fileinput('修改图片');
      				$('.fileinput').fileinput({
      					uploadUrl:'upWorkerImg.php',
      					removeLabel:'删除',
      					browseLabel:'浏览',
      					uploadLabel:'上传',
      					cancelLabel:'撤销',
      					uploadExtraData:{'pid':worker.wId}
      				});
      				$('.kv-fileinput-upload').click(function(){
      					setTimeout(function(){
      						$.post('upWorkerImg.php',{'id':worker.wId},function(o){
      							if(!o.status){
      								$.message('提示','获取数据失败');
      							}else{
      								$('.icon'+worker.wId).attr('src','img/worker/'+o.src);
      								$('.modal').modal('hide');
      							}
      						});
      					},1000);
      				});
      			});

      			var nameSpan = $('<span>',{html:worker.wName});			//名字
      			nameSpan.editable({
      				placement:'bottom',
      				type:'text',
      				url:'upWorkerImg.php',
      				name:'name',
      				pk:worker.wId
      			});
      			nameSpan.appendTo(row.insertCell(-1));

      			var priceSpan = $('<span>',{html:worker.age});			//年龄
      			priceSpan.editable({
      				placement:'bottom',
      				type:'text',
      				url:'upWorkerImg.php',
      				name:'age',
      				pk:worker.wId
      			});
      			priceSpan.appendTo(row.insertCell(-1));

      			var typeSpan = $('<span>',{html:worker.sex});			//性别
      			
      			typeSpan.editable({
	      			placement:'bottom',
	      			type:'select',
	      			url:'upWorkerImg.php',
	      			source:['男','女'],
	      			name:'sex',
	      			pk:worker.wId
      			});
      			typeSpan.appendTo(row.insertCell(-1));

      			var statusSpan = $('<span>',{html:worker.idCard});			//省份证
      			statusSpan.editable({
      				placement:'bottom',
      				type:'number',
      				url:'upWorkerImg.php',
      				name:'idCard',
      				pk:worker.wId
      			});
      			statusSpan.appendTo(row.insertCell(-1));
      			
      			var statusSpan = $('<span>',{html:worker.phone});			//电话
      			statusSpan.editable({
      				placement:'bottom',
      				type:'number',
      				url:'upWorkerImg.php',
      				name:'phone',
      				pk:worker.wId
      			});
      			statusSpan.appendTo(row.insertCell(-1));
      			
      			var statusSpan = $('<span>',{html:worker.location});			//地址
      			statusSpan.editable({
      				placement:'bottom',
      				type:'text',
      				url:'upWorkerImg.php',
      				name:'location',
      				pk:worker.wId
      			});
      			statusSpan.appendTo(row.insertCell(-1));
      			
      			var statusSpan = $('<span>',{html:worker.rName});			//职位
      			statusSpan.editable({
      				placement:'bottom',
      				type:'select',
      				url:'upWorkerImg.php',
      				source:[{'1':'经理'},{'2':'店长'},{'3':'销售员'}],
      				name:'roleId',
      				pk:worker.wId
      			});
      			statusSpan.appendTo(row.insertCell(-1));

      			var deleteSpan = $('<span>',{html:'删除'});			//删除
      			deleteSpan.addClass('delWorker');
      			deleteSpan.click(function(){
      				$.confirm('删除确认',"你确定要删除["+worker.wName+"]?");
      				$('#confirmBtn').click(function(){
          				var index = row.rowIndex;
      					$.post('del.php',{'id':worker.wId,'name':'worker'},function(o){
      						if(!o.status){
      							$.message('消息',o.info);
      						}else{
      							workerTable.deleteRow(index);
      							dialog.modal('hide');
      						}
      					});
      				});
      			});
      			deleteSpan.addClass('deleteSpan').appendTo(row.insertCell(-1));
	  		}
   		</script>
   	</body>
</html>